package com.cn.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cn.entity.Message;
import com.cn.entity.User;
import com.cn.service.IUserService;
import com.cn.service.Imp.UserServiceImp;
import com.cn.vo.userVo.Friend;
import com.cn.vo.userVo.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName Controller
 * @Description TODO
 * @Author yujiajing
 * @Date 2022/11/7 9:08
 * @Version 1.0
 */
@CrossOrigin(origins = "*")
@RestController
public class Controller {
    @Autowired
    IUserService userService;

   @Autowired
    Friend friend;




    @ResponseBody
    @RequestMapping("/login")
    public String login(@RequestParam("UserName") String UserName,String UserPassword){

       if( userService.login(UserName,UserPassword)){
           return  "success";
       }else {
           return  "fail";
       }

    }


    @RequestMapping("/manageLogin")
    @ResponseBody
    public String manageLogin(String managerAdmin ,String managerPassword){
        if(userService.ManageLogin(managerAdmin,managerPassword)){
            return  "success";
        }else {
            return  "fail";
        }
        }


    @RequestMapping("/accountCancel")
    public String down(String Admin){
            userService.deleteUser(Admin);
            return "注销账户成功";
        }
 @ResponseBody
 @RequestMapping("updateUser")
 public String updateUser(User user) {
     userService.updateUser(user);

     return "更新成功";
 }
    @RequestMapping("/showUserByAdmin")
    public Object showUserByAdmin(String userName){
            return userService.showUserById(userName);
    }
    @RequestMapping("/register")
    @ResponseBody
    public  String Register(User user){
        user.setUserId(IdWorker.getId());
        userService.register(user);
        return "注册成功" ;

    }
    @RequestMapping("/beFriendRequest")
    @ResponseBody
    public String beFriendRequest(int requestId, int receiverId) {
        userService.sendBeFriendRequest(requestId,receiverId);
        return "已成功发出对"+ receiverId + "的好友请求,将加入到"+receiverId+"的好友申请列表中";
    }
    @RequestMapping("/beFriend")
    @ResponseBody
    public  String beFriend(String recAdmin,String recName,String reqAdmin,String reqName){
        userService.BeFriend(recAdmin,recName,reqAdmin,reqName);
        return "添加好友成功，"+recAdmin+"与"+reqAdmin+"互相成为对方的好友了";
    }
    @RequestMapping("/refuseBeFriend")
    @ResponseBody
    public String refuse(String recAdmin,String reqAdmin){
        userService.refuseBeFriend(recAdmin,reqAdmin);
        return "拒绝了"+reqAdmin+"的好友申请";
    }
    @RequestMapping("/sendMassage")
    @ResponseBody
    public  String sendMassage(Message message) {
        userService.sendMassageForFriendById(message);
        return "发送成功";
    }
   @RequestMapping("/showUser")
    @ResponseBody
    public  List<User> show(){
       System.out.println("后端接受请求成功！");
     /*  String stringJason=JSON.toJSONString(userService.showAll());
       System.out.println(stringJason);*/
        return userService.showAll();
   }
   @RequestMapping("showMassage")
    @ResponseBody
    public List<Message> showMassage(String receiverAdmin){
       return userService.showMassage(receiverAdmin);
   }
    @RequestMapping("showMassageBySender")
    @ResponseBody
    public List<Message> showMassage(String senderAdmin, String receiverAdmin){
        return userService.showMassageBySender(senderAdmin,receiverAdmin);
    }
    @RequestMapping("beenRead")
    @ResponseBody
    public String readMassage(int receiverAdmin){
        userService.hasBeenRead(receiverAdmin);
        return "全部已读成功";
    }
    @RequestMapping("showFriend")
    @ResponseBody
    public  List<Friend> showFriend(String userName){
        return  userService.showFriendById(userName);
    }
    @RequestMapping("deleteFriendByAdmin")
    @ResponseBody
    public  String showFriend(String userName,String friendUsername){
          userService.deleteFriendByAdmin(userName,friendUsername);
          return  "删除成功!";
    }
    @RequestMapping("showRequest")
    @ResponseBody
    public List<Request> showRequest(String receiveAdmin){
       return userService.showRequest(receiveAdmin);
    }

    @RequestMapping("sendReguest")
    @ResponseBody
    public String sendRequest(String recAdmin,String recName,String reqAdmin,String reqName){
        userService.sendRequest(reqAdmin,reqName,recAdmin,recName);
       return "成功向"+recAdmin+"发送添加好友请求";
    }
    @RequestMapping("flexibleQueryUser")
    @ResponseBody
    public List<User> flexibleQueryUser(String query) {
        return userService.flexibleUser(query);
    }
    @RequestMapping("flexibleQueryMessage")
    @ResponseBody
    public List<Message> flexibleQuerMessage(String message) {
        return userService.flexibleQuery(message);
    }
    @RequestMapping("showAllMessage")
    @ResponseBody
    public List<Message> showAllMessage(){
        return userService.showAllMessage();
    }


}
